//
//  ZPCustomScrollView.m
//  ZPScrollView
//
//  Created by zkingsoft on 16/1/13.
//  Copyright (c) 2016年 zkingsoft. All rights reserved.
//
#define VIEW_WIDTH self.frame.size.width
#define VIEW_HEIGHT self.frame.size.height


#import "ZPCustomScrollView.h"

#import "NSTimer+Action.h"

@interface ZPCustomScrollView (){
    UIImageView     *_previousView;
    UIImageView     *_currentView;
    UIImageView     *_nextView;
    
    
    NSTimer         *_timer;// 定时器
}

@end

@implementation ZPCustomScrollView

#pragma mark - 初始化frame
- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        //设置scrollview
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT)];
        _scrollView.delegate = self;
        _scrollView.contentSize = CGSizeMake(VIEW_WIDTH * 3, VIEW_HEIGHT);
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.backgroundColor = [UIColor blackColor];
        [self addSubview:_scrollView];
        
        //设置分页
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, VIEW_HEIGHT-30, VIEW_WIDTH, 30)];
        _pageControl.userInteractionEnabled = NO;
        _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
        _pageControl.pageIndicatorTintColor = [UIColor blueColor];
        [self addSubview:_pageControl];
        
        
        // 为scrollView添加点击手势
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(contentViewTapAction:)];
        [_scrollView addGestureRecognizer:tapGesture];// 为视图添加 点击手势
    }
    return self;
}


#pragma mark - 当点击scrollview的时候调用Block
- (void)contentViewTapAction:(UITapGestureRecognizer *)tap{
    if (self.TapActionBlock) {
        self.TapActionBlock(_currentPage);
    }
}


#pragma mark scrollvie停止滑动调用
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [_timer pauseTimer];
}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //得到当前页数
    float x = _scrollView.contentOffset.x;
    
    //往前翻
    if (x<=0) {
        if (_currentPage-1<0) {
            _currentPage = _imageViewArray.count-1;
        }else{
            _currentPage --;
        }
    }
    
    //往后翻
    if (x>=VIEW_WIDTH*2) {
        if (_currentPage==_imageViewArray.count-1) {
            _currentPage = 0;
        }else{
            _currentPage ++;
        }
    }
    
    [self reloadData];
    
    [_timer resumeTimerAfterTimeInterval:3];
}


#pragma mark - 设置视图数组的set方法
- (void)setImageViewArray:(NSMutableArray *)imageViewArray{
    
    if (imageViewArray) {
        _imageViewArray = imageViewArray;
        
        _currentPage = 0; // 设置默认的页数为第一页
        
        _pageControl.numberOfPages = _imageViewArray.count;// 设置page个数
    }
    
    [self reloadData];// 当设置完 视图数据后 加载 需要显示的那三个视图并添加到scrollView上面
}


#pragma mark - 刷新数据 这里是设置左 中 右 三个视图
- (void)reloadData{
    [_previousView removeFromSuperview];
    [_currentView removeFromSuperview];
    [_nextView removeFromSuperview];
    
    //从数组中取到对应的图片view加到已定义的三个view中
    if (_currentPage == 0) {
        _previousView = [_imageViewArray lastObject];
        _currentView = [_imageViewArray objectAtIndex:_currentPage];
        _nextView = [_imageViewArray objectAtIndex:_currentPage+1];
    }else if (_currentPage == _imageViewArray.count-1){
        _previousView = [_imageViewArray objectAtIndex:_currentPage-1];
        _currentView = [_imageViewArray objectAtIndex:_currentPage];
        _nextView = [_imageViewArray firstObject];
    }else{
        _previousView = [_imageViewArray objectAtIndex:_currentPage-1];
        _currentView = [_imageViewArray objectAtIndex:_currentPage];
        _nextView = [_imageViewArray objectAtIndex:_currentPage+1];
    }
    
    _previousView.frame = CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT);
    _currentView.frame = CGRectMake(VIEW_WIDTH, 0, VIEW_WIDTH, VIEW_HEIGHT);
    _nextView.frame = CGRectMake(2*VIEW_WIDTH, 0, VIEW_WIDTH, VIEW_HEIGHT);
    
    [_scrollView addSubview:_previousView];
    [_scrollView addSubview:_currentView];
    [_scrollView addSubview:_nextView];
    
    // 跟新pageControl 将scrollView显示中间的View
    _pageControl.currentPage = _currentPage;
    _scrollView.contentOffset = CGPointMake(VIEW_WIDTH, 0);// 动画结束后 移动contentOffset到中心视图
}

#pragma mark - 自动滚动
- (void)shouldAutoShowNextPage:(BOOL)shouldStart{
    if (shouldStart)  //开启自动翻页
    {
        if (!_timer) {
            _timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(autoShowNextImage) userInfo:nil repeats:YES];
        }
    }
    else   //关闭自动翻页
    {
        if (_timer.isValid) {
            [_timer invalidate];
            _timer = nil;
        }
    }
}

#pragma mark 展示下一页
-(void)autoShowNextImage
{
    if (_currentPage == _imageViewArray.count-1) {
        _currentPage = 0;
    }else{
        _currentPage ++;
    }
    
    [self reloadData];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
